package com.kjsb.controller;

import com.kjsb.entity.ProjectCategory;
import com.kjsb.service.ProjectCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目类表现层
 * @author shi lin tong
 * @data 2022年 11月 25日15:00
 */
@RestController
@CrossOrigin
@RequestMapping("/ProjectCategorys")
public class ProjectCategoryController {
    @Autowired
    private ProjectCategoryService projectCategoryService;
    /**
     * 查询所有项目类信息
     * @return Result结果集
     */
    @GetMapping
    public Result getAllProjectCategorys(){
        List<ProjectCategory> list = projectCategoryService.getAll();
        Integer code=list != null ? Code.GET_OK : Code.GET_ERR;
        String msg= list != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }
    /**
     * 添加项目类别
     * @return Result结果集
     */
    @PostMapping
    public Result saveProjectCategorys(@RequestBody ProjectCategory projectCategory){
        Boolean flag = projectCategoryService.save(projectCategory);
        Integer code=flag?Code.SAVE_OK:Code.SAVE_ERR;
        String msg=flag?"添加成功":"添加失败，请重试";
        return new Result(code,flag,msg);
    }
    /**
     * 修改项目类别
     * @return Result结果集
     */
    @PutMapping
    public Result updateProjectCategorys(@RequestBody ProjectCategory projectCategory){
        Boolean flag = projectCategoryService.updateCategoryName(projectCategory);
        Integer code=flag?Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg=flag?"修改成功":"修改失败，请重试";
        return new Result(code,flag,msg);
    }
    /**
     * 根据id查询项目类别
     * @return Result结果集
     */
    @GetMapping("/{id}")
    public Result getProjectCategorys(@PathVariable("id") int id){
        ProjectCategory one = projectCategoryService.getOneById(id);
        Integer code=one != null ? Code.GET_OK : Code.GET_ERR;
        String msg= one != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,one,msg);
    }
    /**
     * 根据id删除项目类别
     * @return Result结果集
     */
    @DeleteMapping("/{id}")
    public Result deleteProjectCategorys(@PathVariable("id") int id){
        Boolean flag = projectCategoryService.removeById(id);
        Integer code=flag?Code.DELETE_OK:Code.DELETE_ERR;
        String msg=flag?"删除成功":"删除失败，请重试";
        return new Result(code,flag,msg);
    }

}
